package com.hymjweb.demo.hymj.customer_manage.controller;

import com.hymjweb.demo.framework.result.BaseResultMap;
import com.hymjweb.demo.framework.sys.BaseController;
import com.hymjweb.demo.framework.sys.SysConstant;
import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.hymj.customer_manage.bean.BCustomerBean;
import com.hymjweb.demo.hymj.customer_manage.bean.BCustomerPojo;
import com.hymjweb.demo.hymj.customer_manage.service.BCustomerService;
import com.hymjweb.demo.hymj.system_maintenance.user.bean.XtUserPojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/pages/customer_manage")
public class BCustomerController extends BaseController {

    @Autowired
    private BCustomerService bCustomerService;

    @RequestMapping("/init")
    public String init(HttpServletRequest request) {
        return "pages/customer_manage/bcustomer";
    }

    @RequestMapping("/query")
    @ResponseBody
    public Object query(@RequestParam(required = false) Map param) throws Exception {
        return bCustomerService.getPageList(param);
    }

    /**
     * 根据客户名称，拼音，简拼等查询客户信息,注意返回的列表不分页, searchField="key"
     * @param key
     * @return
     */
    @RequestMapping("/queryDeliveryCustomerByKey")
    @ResponseBody
    public List<BCustomerPojo> queryDeliveryCustomerByKey(String key) {
        if (DataTypeUtil.isEmptyStr(key)) {
            return null;
        }
        List<BCustomerPojo> customerPojoList = bCustomerService.queryDeliveryCustomerByKey(key);
        return customerPojoList;
    }

    /**
     * 根据key查询客户表中包含的业务员列表
     * @param key
     * @return
     */
    @RequestMapping("/queryDeliveryCustomerSalesmanByKey")
    @ResponseBody
    public List<XtUserPojo> queryDeliveryCustomerSalesmanByKey(String key) {
        if (DataTypeUtil.isEmptyStr(key)) {
            return null;
        }
        List<XtUserPojo> salesmanPojoList = bCustomerService.queryDeliveryCustomerSalesmanByKey(key);
        return salesmanPojoList;
    }

    @RequestMapping("/findOne")
    @ResponseBody
    public Object findOne(@RequestParam(required = false) String bCustomerId) throws Exception {
        if (DataTypeUtil.isEmptyStr(bCustomerId)) {
            throwException("客户查询参数错误");
        }
        return bCustomerService.queryById(bCustomerId);
    }

    @RequestMapping("/insert")
    @ResponseBody
    public Object insert(@RequestBody(required = true) BCustomerBean bean) throws Exception {
        if (bean == null) {
            throwException("客户添加参数错误!");
        }
        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("客户添加成功!");

        int affectRows = bCustomerService.insert(bean);
        if (affectRows <= SysConstant.INT_ZERO) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("客户添加失败!");
        }
        return baseResultMap;
    }

    @RequestMapping("/update")
    @ResponseBody
    public Object update(@RequestBody(required = true) BCustomerBean bean) throws Exception {
        if (bean == null) {
            throwException("客户更新参数错误!");
        }

        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("客户更新成功!");

        int affectRows = bCustomerService.update(bean);
        if (affectRows <= SysConstant.INT_ZERO) {
            baseResultMap.setOk(true);
            baseResultMap.setMessage("客户更新失败!");
        }

        return baseResultMap;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(@RequestBody(required = true) String id) throws Exception {
        if (DataTypeUtil.isEmptyStr(id)) {
            throwException("客户删除参数错误!");
        }

        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("客户删除成功!");

        int affectRows = bCustomerService.delete(id);
        if (affectRows <= SysConstant.INT_ZERO) {
            baseResultMap.setOk(true);
            baseResultMap.setMessage("客户删除失败!");
        }
        return baseResultMap;
    }

}
